/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr;

import java.io.IOException;
import java.io.Writer;
import org.apache.solr.core.PluginBag;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.TextQueryResponseWriter;
import org.junit.BeforeClass;
import org.junit.Test;

/** Tests the ability to configure multiple query output writers, and select those at query time. */
public class OutputWriterTest extends SolrTestCaseJ4 {

  /** The XML string that's output for testing purposes. */
  public static final String USELESS_OUTPUT = "useless output";

  @BeforeClass
  public static void beforeClass() throws Exception {
    initCore("solr/crazy-path-to-config.xml", "solr/crazy-path-to-schema.xml");
  }

  /**
   * responseHeader has changed in SOLR-59, check old and new variants, In SOLR-2413, we removed
   * support for the deprecated versions
   */
  @Test
  public void testSOLR59responseHeaderVersions() {
    // default results in "new" responseHeader
    lrf.args.put("wt", "standard");
    assertQ(req("foo"), "/response/lst[@name='responseHeader']/int[@name='status'][.='0']");
    lrf.args.remove("wt");
    assertQ(req("foo"), "/response/lst[@name='responseHeader']/int[@name='QTime']");
  }

  @Test
  public void testUselessWriter() throws Exception {
    lrf.args.put("wt", "useless");
    String out = h.query(req("foo"));
    assertEquals(USELESS_OUTPUT, out);
  }

  public void testLazy() {
    PluginBag.PluginHolder<QueryResponseWriter> qrw =
        h.getCore().getResponseWriters().getRegistry().get("useless");
    assertTrue("Should be a lazy class", qrw instanceof PluginBag.LazyPluginHolder);

    qrw = h.getCore().getResponseWriters().getRegistry().get("xml");
    assertTrue("Should not be a lazy class", qrw.isLoaded());
    assertSame("Should not be a lazy class", qrw.getClass(), PluginBag.PluginHolder.class);
  }

  ////////////////////////////////////////////////////////////////////////////
  /** An output writer that doesn't do anything useful. */
  public static class UselessOutputWriter implements TextQueryResponseWriter {

    public UselessOutputWriter() {}

    @Override
    public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response)
        throws IOException {
      writer.write(USELESS_OUTPUT);
    }

    @Override
    public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
      return CONTENT_TYPE_TEXT_UTF8;
    }
  }
}
